package com.xydtech.ecard.config.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.constant.UserConstants;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardBusiReportConfig;
import com.xydtech.ecard.config.mapper.EcardBusiReportConfigMapper;
import com.xydtech.ecard.config.service.IEcardBusiReportConfigService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片业务填报类型配置Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-25 上午10:21
 */
@Service("busiReportConfig")
public class EcardBusiReportConfigServiceImpl implements IEcardBusiReportConfigService
{
    @Autowired
    private EcardBusiReportConfigMapper ecardBusiReportConfigMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片业务填报类型配置
     *
     * @param id 电子名片业务填报类型配置主键
     * @return 电子名片业务填报类型配置
     */
    @Override
    public EcardBusiReportConfig selectEcardBusiReportConfigById(Long id)
    {
        return ecardBusiReportConfigMapper.selectEcardBusiReportConfigById(id);
    }

    /**
     * 查询电子名片业务填报类型配置列表
     *
     * @param ecardBusiReportConfig 电子名片业务填报类型配置
     * @return 电子名片业务填报类型配置
     */
    @Override
    @DataScope(deptAlias = "a", userAlias = "u")
    public List<EcardBusiReportConfig> selectEcardBusiReportConfigList(EcardBusiReportConfig ecardBusiReportConfig)
    {
        return ecardBusiReportConfigMapper.selectEcardBusiReportConfigList(ecardBusiReportConfig);
    }

    /**
     * 新增电子名片业务填报类型配置
     *
     * @param ecardBusiReportConfig 电子名片业务填报类型配置
     * @return 结果
     */
    @Override
    public int insertEcardBusiReportConfig(EcardBusiReportConfig ecardBusiReportConfig)
    {
        return ecardBusiReportConfigMapper.insertEcardBusiReportConfig(ecardBusiReportConfig);
    }

    /**
     * 修改电子名片业务填报类型配置
     *
     * @param ecardBusiReportConfig 电子名片业务填报类型配置
     * @return 结果
     */
    @Override
    public int updateEcardBusiReportConfig(EcardBusiReportConfig ecardBusiReportConfig)
    {
        //是多倍积分把积分值改为null
        if ("T".equals(ecardBusiReportConfig.getIsDpsOrLoan())){
            ecardBusiReportConfig.setIntegrationNums(null);
        }
        return ecardBusiReportConfigMapper.updateEcardBusiReportConfig(ecardBusiReportConfig);
    }

    /**
     * 批量删除电子名片业务填报类型配置
     *
     * @param ids 需要删除的电子名片业务填报类型配置主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportConfigByIds(String ids)
    {
        return ecardBusiReportConfigMapper.deleteEcardBusiReportConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片业务填报类型配置信息
     *
     * @param id 电子名片业务填报类型配置主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportConfigById(Long id)
    {
        return ecardBusiReportConfigMapper.deleteEcardBusiReportConfigById(id);
    }

    /**
     * 校验业务唯一标识是否唯一
     * @param ecardBusiReportConfig
     * @return
     */
    @Override
    public String checkBusiCodeUnique(EcardBusiReportConfig ecardBusiReportConfig) {
        Long id = StringUtils.isNull(ecardBusiReportConfig.getId()) ? -1L : ecardBusiReportConfig.getId();
        EcardBusiReportConfig reportConfig = ecardBusiReportConfigMapper.checkBusiCodeUnique(ecardBusiReportConfig.getBusiCode(),
                ecardBusiReportConfig.getDeptId());
        if (StringUtils.isNotNull(reportConfig) && reportConfig.getId().longValue() != id.longValue())
        {
            return UserConstants.CONFIG_KEY_NOT_UNIQUE;
        }
        return UserConstants.CONFIG_KEY_UNIQUE;
    }

    /**
     * 根据业务填报分类查询业务填报类型列表
     * @param busiType
     * @return
     */
    @Override
    public List<EcardBusiReportConfig> selectEcardBusiReportConfigByBusiType(String busiType) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        EcardBusiReportConfig reportConfig = new EcardBusiReportConfig();
        reportConfig.setBusiType(Integer.valueOf(busiType));
        reportConfig.setDeptId(sysDept.getDeptId());
        return ecardBusiReportConfigMapper.selectEcardBusiReportConfigList(reportConfig);
    }
}
